﻿//这段程序代码主要是向读者展示四种不同的对话框的创建方法
package _8_JDialog;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class TJOptionPane extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    static final int WIDTH = 700;
    static final int HEIGHT = 400;

    TJOptionPane() {
        JFrame f = new JFrame("对话框测试窗口");
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int width = screenSize.width;
        int height = screenSize.height;
        int x = (width - WIDTH) / 2;
        int y = (height - HEIGHT) / 2;
        f.setLocation(x, y);
        f.setSize(WIDTH, HEIGHT);
        f.setContentPane(this);
        setLayout(new FlowLayout());

        JButton b1 = new JButton("showMessageDialog");// 创建四个按钮组件，这四个按钮组件引发的动作事件是显示出四个不同类型的对话框
        JButton b2 = new JButton("showConfirmDialog");
        JButton b3 = new JButton("showOptionDialog");
        JButton b4 = new JButton("showInputDialog");
        add(b1);
        add(b2);
        add(b3);
        add(b4);
        b1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent Event) {

                JOptionPane.showMessageDialog(null, "这是一个消息对话框");// 这是一个showMessageDialog静态方法显示的对话框
            }
        });
        b2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent Event) {

                int result = JOptionPane.showConfirmDialog(null, "这是一个有关confirm的消息", "这是一个有关confirm的对话框",
                        JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE); // 这是一个showConfirmDialog静态方法显示的对话框
                System.out.print("ConfirmDialog:" + result + "  ");
                switch (result) {
                case JOptionPane.OK_OPTION:
                    System.out.println("OK");
                    break;
                case JOptionPane.CANCEL_OPTION:
                    System.out.println("CANCEL");
                    break;
                case JOptionPane.NO_OPTION:
                    System.out.println("NO");
                    break;
                default:
                    System.out.println("没有选择");
                }
            }
        });
        b3.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent Event) {

                Object[] options = { "跳楼", "撞车", "跳海", "上吊" };
                int result = JOptionPane.showOptionDialog(null, "点击ok按钮继续", "警告", JOptionPane.DEFAULT_OPTION,
                        JOptionPane.WARNING_MESSAGE, null, options, options[0]);
                System.out.println("OptionDialog:" + result);

            }// 这是一个showOptionDialog静态方法显示的对话框
        });
        b4.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent Event) {

                Object[] options = { "第一", "第二", "第三" };
                String result = JOptionPane.showInputDialog(null, "请选择一个", "输入", JOptionPane.INFORMATION_MESSAGE,
                        new ImageIcon("blue.gif"), options, options[0]).toString();
                System.out.println("InputDialog:" + result);
                result = JOptionPane.showInputDialog("Hello");
                System.out.println("InputDialog:" + result);
            }// 这是一个showInputDialog静态方法显示的对话框
        });
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        new TJOptionPane();
    }
}
// messageType
// 定义 message 的样式。外观管理器布置的对话框可能因此值而异，并且往往提供默认图标。可能的值为：
// ERROR_MESSAGE
// INFORMATION_MESSAGE
// WARNING_MESSAGE
// QUESTION_MESSAGE
// PLAIN_MESSAGE
//
// optionType
// 定义在对话框的底部显示的选项按钮的集合：
// DEFAULT_OPTION
// YES_NO_OPTION
// YES_NO_CANCEL_OPTION
// OK_CANCEL_OPTION
